#include "PublicFunHelper.h"
#include <QJsonDocument>
#include "modules/video_capture/video_capture.h"
#include "modules/video_capture/video_capture_factory.h"

PublicFunHelper::PublicFunHelper()
{

}

QString PublicFunHelper::getRandomString(const int &iLen)
{
    static const QString charSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    static int strLen = charSet.length();

    QString randomString;
    for (int i = 0; i < iLen; i++)
    {
        randomString += charSet.at(qrand() % strLen);
    }
    return randomString;
}

QJsonObject PublicFunHelper::convertToJson(const QString &strMsg)
{
    QJsonDocument doc = QJsonDocument::fromJson(strMsg.toLocal8Bit());
    if(doc.isObject())
    {
        return  doc.object();
    }

    return  QJsonObject();
}

QString PublicFunHelper::convertToString(const QJsonObject &json)
{
    return  QString(QJsonDocument(json).toJson(QJsonDocument::Compact));
}

void PublicFunHelper::getCameraDevices(QStringList &name, QStringList &id)
{
    std::unique_ptr<webrtc::VideoCaptureModule::DeviceInfo> info = nullptr;
    info.reset(webrtc::VideoCaptureFactory::CreateDeviceInfo());

    if(info.get() != nullptr)
    {
        int iNum = info->NumberOfDevices();
        for(int i = 0; i < iNum; i++)
        {
            const int iSize = 256;
            char szName[iSize] = {0};
            char szID[iSize] = {0};
            if(info->GetDeviceName(i, szName, iSize, szID, iSize) != -1)
            {
                 name.append(QString::fromLatin1(szName));
                 id.append(QString::fromLatin1(szID));
            }
        }
    }
}
